<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\models\Advert;

class AdvertController extends Controller
{

    public function upload(Request $request)
    {
//       $input = $request->all();
//       dd($input);
//       获取上传文件
       $file =  $request->file('file_upload');
      //dd($file);



        //判断上传文件是否有效
        if($file->isValid()){
//            获取上传文件的扩展名
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
//            生成新文件名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;


            //将文件移动到本服务器指定的位置
            // $file->move(public_path().'/uploads',$newName);

            //将文件移动到七牛云服务器上
            \Storage::disk('qiniu')->writeStream('uploads/'.$newName, fopen($file->getRealPath(), 'r'));


            //上传到阿里OSS上
//            OSS::upload(上传到服务器上的新文件名，上传到临时文件夹的图片的地址);
            // OSS::upload('uploads/'.$newName,$file->getRealPath());

//            将文件保存的路径返回给前台
            $filepath = 'uploads/'.$newName;
            return  $filepath;
        }
    }
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        // 接收搜索内容
        $keywords = $request->input('keywords') ? $request->input('keywords') : '';

        // 接收分页页数
        $count = $request->input('count') ? $request->input('count') : '3';

        // 接收所有参数
        $res = $request -> all();
        // 按条件查询信息
        $data = Advert::where('aname','like','%'.$keywords.'%')->paginate($count);
        // dd($data);
        // $data = Advert::all();


        return view('admin.advert.index',compact('data','res','count','keywords'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        
        return view('admin.advert.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
       $input = $request->except('_token','file_upload');

       // dd($input);

       $advert = new Advert;
       $advert -> aname = $input['aname'];
       $advert -> url = $input['url'];
       $advert -> log = $input['log'];
       
        if($advert->save()){
                //如果成功，跳转到列表页
                return redirect('admin/advert');
            }else{
                //如果失败，返回添加页
                return back()->with('msg','添加失败');
            }

        }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $data = Advert::find($id);
        //dd($art);

        return view('admin.advert.edit',compact('data'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

     public function editUpload(Request $request, $id)
     {
         $input = $request->except('_token','file_upload');

         // dd($input);
         
         $advert = Advert::find($id);
         $advert -> aname = $input['aname'];
         $advert -> url = $input['url'];
         $advert -> log = $input['log'];
        // dd($advert);
        if($advert->save()){
                //如果成功，跳转到列表页
                return redirect('admin/advert');
            }else{
                //如果失败，返回添加页
                return back()->with('msg','添加失败');
            }
     }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $data = [];
        $res = Advert::find($id)->delete();
        if($res){
            $data=[
                'status'=>0,
                'msg'=>'删除成功'
            ];
        }else{
            $data=[
                'status'=>1,
                'msg'=>'删除失败'
            ];
        }
        return $data;
    }
}
